<template>
    <el-upload class="avatar-uploader" :action="action" :show-file-list="false"
        :on-success="handleAvatarSuccess" >
        <img v-if="modelValue" :src="IMG_BASE_URL+modelValue" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon">
            <Plus />
        </el-icon>
    </el-upload>
</template>

<script lang="ts" setup>
import { ref} from 'vue'
import { Plus } from '@element-plus/icons-vue'

import type { UploadProps } from 'element-plus'

 const modelValue= defineModel();

// const props = defineProps<{
//     modelValue: string
// }>()

// const emit = defineEmits(['update:modelValue'])

const action = import.meta.env.VITE_API_BASE_URL + '/api/upload/add';
const IMG_BASE_URL = import.meta.env.VITE_IMG_BASE_URL;

 

const handleAvatarSuccess: UploadProps['onSuccess'] = (
    response,
) => {

    console.log("response",response.data.url);

    if(response.code == 10000){

        modelValue.value=response.data.url;

        // emit('update:modelValue', response.data.url)

    }
    
  
}

 
</script>

<style scoped>
.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>